//
//  LYQTopicListViewController.m
//  ZCProject
//
//  Created by Zheng on 2016/12/20.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQTopicListViewController.h"

#import "LYQTopicListCell.h"
#import "LYQTopicListNumberCell.h"
#import "LYQTopicListSearchView.h"
#import "LYQTopicListSelectedView.h"

#define kLYQTopicListCell_ID @"kLYQTopicListCell_ID"
#define kLYQTopicListNumberCell_ID @"kLYQTopicListNumberCell_ID"

@interface LYQTopicListViewController () <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic, strong) LYQTopicListSearchView *headerView; //头视图-搜索
@property (nonatomic, strong) LYQTopicListSelectedView *selectedView; //类型选择 (最新/关注)

@end

@implementation LYQTopicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"话题";
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    self.listTableView.estimatedRowHeight = 100;
    self.listTableView.rowHeight = UITableViewAutomaticDimension;
    [self.listTableView registerNib:[UINib nibWithNibName:@"LYQTopicListCell" bundle:nil] forCellReuseIdentifier:kLYQTopicListCell_ID];
    [self.listTableView registerClass:[LYQTopicListNumberCell class] forCellReuseIdentifier:kLYQTopicListNumberCell_ID];
    
    [self.view addSubview:self.selectedView];
    
    [self selectedTypeAction];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(kWScale(50)));
    }];
    
    [self.listTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(kWScale(50));
    }];
}

#pragma mark - lazy load subviews
- (LYQTopicListSelectedView *)selectedView {
    if (_selectedView) {
        return _selectedView;
    }
    
    _selectedView = [[LYQTopicListSelectedView alloc] init];
    
    return _selectedView;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        LYQTopicListCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQTopicListCell_ID forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            cell.infoLbl.text = @"按实际的拉伸觉得快拉丝机的卢卡斯建档立卡见识到了就爱上了肯德基";
        } else {
            cell.infoLbl.text = @"啊掉身价库拉索建档立卡手机登录卡机了的开始就爱上了肯德基阿拉斯加的老卡机的考拉手机登录卡斯加达拉斯建档立卡就是";
        }
        
        return cell;
    } else {
        
        LYQTopicListNumberCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQTopicListNumberCell_ID forIndexPath:indexPath];
        
        return cell;
    }
    
}

#pragma mark - tableview delegate
#pragma mark 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"点击section=%ld",indexPath.section);
}

#pragma mark 尾视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001f;
}

#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kWScale(50);
    } else {
        return kWScale(10);
    }
}

#pragma mark 自定义头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        if (_headerView) {
            return _headerView;
        }
        
        _headerView = [[LYQTopicListSearchView alloc] initWithFrame:CGRectMake(kWScale(10), kWScale(10), kMS_Width-kWScale(20), kWScale(30))];
        _headerView.searchBar.delegate = self;
        
        return _headerView;
        
    } else {
        return nil;
    }
}

#pragma mark 设置分割线偏移量
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark - search bar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    NSLog(@"我点了");
    //TODO: 感觉应该跳转新的搜索输入页面,不然键盘不好控制
    return NO;
}

#pragma mark - 最新/关注 点击事件
- (void)selectedTypeAction {
    [_selectedView chooseButtonTypeWithChooseBlock:^(NSInteger index) {
        
        if (index == 0) {
            NSLog(@"点击最新");
        } else {
            NSLog(@"点击关注");
        }
        
    }];
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
